package com.woniu.jifen.controller;

import com.woniu.common.util.PageParam;
import com.woniu.common.util.ResponseData;
import com.woniu.jifen.entity.ACoupon;
import com.woniu.jifen.entity.TProductSort;
import com.woniu.jifen.service.ACouponService;
import com.woniu.jifen.service.AUserCouponService;
import com.woniu.jifen.service.TProductSortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @Author ：sw
 * @Date 2024年05月22日   19:02
 * @Decription
 */
@RestController
@Api(tags = "优惠券API")
public class CouponController {
    @Autowired
    private TProductSortService sortService;
    @Autowired
    private AUserCouponService userCouponService;

    /**
     * 查优惠券种类
     */
    @PostMapping("/querySort")
    @ApiOperation(value = "获取优惠券种类",notes = "获取优惠券种类")
    public ResponseData<TProductSort> querySory() {
        List<TProductSort> sortList = sortService.list();
        return new ResponseData<>().ok(sortList);
    }

    @Autowired
    private ACouponService couponService;

    /**
     * 查询指定种类的优惠券
     */
    @PostMapping("/queryCouponBySortId")
    @ApiOperation(value = "查询指定种类的优惠券",notes = "查询指定种类的优惠券")
    private ResponseData<?> queryCouponBySortId(@RequestBody PageParam<ACoupon> pageParam){
        return couponService.queryCouponBySortId(pageParam);
    }

    /**
     * 购买指定的优惠券
     */
    @GetMapping("/buyCoupon/{id}")
    @SneakyThrows
    @ApiOperation(value = "购买指定的优惠券",notes = "购买指定的优惠券")
    private ResponseData<?> buyCouponById(@PathVariable("id") Integer id){

        return couponService.buyCouponById(id);
    }

    /**
     * 查询用户拥有的优惠券
     */
    @GetMapping("/queryUsersCoupon/{userId}")
    @ApiOperation(value = "查询用户拥有的优惠券",notes = "查询用户拥有的优惠券")
    private ResponseData<?> queryUsersCoupon(@PathVariable("userId") Integer userId){
        return userCouponService.queryByUserId(userId);
    }
    /**
     * 查询用户拥有的优惠券
     */
    @GetMapping("/queryUsersCouponBySortId/{userId}/{sortId}")
    @ApiOperation(value = "查询用户拥有的某种优惠券",notes = "查询用户拥有的某种优惠券")
    private ResponseData<?> queryUsersCouponBySortId(@PathVariable("userId") Integer userId,@PathVariable("sortId") Integer sortId){


        return userCouponService.queryUsersCouponBySortId(userId,sortId);
    }

    /**
     * 使用用户拥有的优惠券
     * @param id 用户持有的优惠券id
     * @return
     */
    @GetMapping("/updateUsersCoupon/{id}")
    @ApiOperation(value = "使用用户拥有的某张优惠券",notes = "使用用户拥有的某张优惠券")
    private ResponseData<?> updateUsersCoupon(@PathVariable("id") Integer id){
        return userCouponService.updateUsersCoupon(id);
    }

    /**
     * 回滚使用的优惠券
     * @param id 用户持有的优惠券id
     * @return
     */
    private void rollbackUsersCoupon(@PathVariable("id") Integer id){
         userCouponService.rollbackUsersCoupon(id);
    }

    /**
     * 根据id查优惠卷
     */
    @PostMapping("/queryCouponById")
    public ACoupon queryCouponById(@RequestParam("id")Integer id){
        return couponService.getById(id);
    }

    /**
     * 查所有优惠券(用于积分抽奖)
     */
    @GetMapping("/queryAll")
    public List<ACoupon> queryAll(String phone){
        return couponService.queryAll(phone);
    }


    @GetMapping("/getPrizeId")
    public ResponseData<?> getPrizeId(String phone) {
        return couponService.getPrizeId(phone);
    }


    /**
     * 购买指定的优惠券
     */
    @GetMapping("/buyCoupon1")
    @SneakyThrows
    @ApiOperation(value = "购买指定的优惠券",notes = "购买指定的优惠券")
    private ResponseData<?> buyCoupon1(Integer id,String phone){

        return couponService.buyCoupon1(id,phone);
    }

    /**
     * 查所有优惠券(用于兑换)
     */
    @GetMapping("/queryAllCoupons")
    public List<ACoupon> queryAllCoupons(){
        return couponService.queryAllCoupons();
    }


}
